APPNAME=xfce4-panel-profiles
VERSION=1.0.8
PREFIX=/usr
PYTHON=`which python`
LANGUAGE_FILES=$(patsubst po/%.po, locale/%/LC_MESSAGES/$(APPNAME).mo, $(wildcard po/*.po))
DESTDIR=

all: $(LANGUAGE_FILES) layouts
	intltool-merge -d po xfce4-panel-profiles.desktop.in xfce4-panel-profiles.desktop
	intltool-merge -x po data/metainfo/org.xfce.PanelProfiles.appdata.xml.in \
		data/metainfo/org.xfce.PanelProfiles.appdata.xml
	chmod +x xfce4-panel-profiles.desktop
	sed -e s,%prefix%,$(PREFIX), bin/$(APPNAME).in.in > bin/$(APPNAME).in
	sed -e s,%python%,$(PYTHON), bin/$(APPNAME).in > bin/$(APPNAME)
	chmod +x bin/$(APPNAME)

locale/%/LC_MESSAGES/$(APPNAME).mo: po/%.po
	mkdir -p $(dir $@)
	msgfmt $< -o $@

pot:
	cd po; intltool-update --pot --gettext-package=xfce4-panel-profiles

layouts:
	cd data/layouts/gnome2; tar -cvjf "../GNOME 2.tar.bz2" *
	cd data/layouts/redmond; tar -cvjf "../Redmond.tar.bz2" *
	cd data/layouts/xfce-4.12; tar -cvjf "../Xfce 4.12.tar.bz2" *
	cd data/layouts/xubuntu-precise; tar -cvjf "../Xubuntu Precise.tar.bz2" *
	cd data/layouts/xubuntu-trusty; tar -cvjf "../Xubuntu Trusty.tar.bz2" *
	cd data/layouts/xubuntu-bionic; tar -cvjf "../Xubuntu Bionic.tar.bz2" *

install: all
	install -d $(DESTDIR)/$(PREFIX)/bin
	install bin/$(APPNAME) $(DESTDIR)/$(PREFIX)/bin

	install -d $(DESTDIR)/$(PREFIX)/share/$(APPNAME)/xfce4-panel-profiles
	install xfce4-panel-profiles/panelconfig.py $(DESTDIR)/$(PREFIX)/share/$(APPNAME)/xfce4-panel-profiles
	install xfce4-panel-profiles/xfce4-panel-profiles.py $(DESTDIR)/$(PREFIX)/share/$(APPNAME)/xfce4-panel-profiles
	install xfce4-panel-profiles/xfce4-panel-profiles.glade $(DESTDIR)/$(PREFIX)/share/$(APPNAME)/xfce4-panel-profiles

	install -d $(DESTDIR)/$(PREFIX)/share/doc/$(APPNAME)
	install AUTHORS $(DESTDIR)/$(PREFIX)/share/doc/$(APPNAME)
	install COPYING $(DESTDIR)/$(PREFIX)/share/doc/$(APPNAME)
	install NEWS $(DESTDIR)/$(PREFIX)/share/doc/$(APPNAME)
	install INSTALL $(DESTDIR)/$(PREFIX)/share/doc/$(APPNAME)
	install README $(DESTDIR)/$(PREFIX)/share/doc/$(APPNAME)

	install -d $(DESTDIR)/$(PREFIX)/share/applications
	install -m 644 $(APPNAME).desktop $(DESTDIR)/$(PREFIX)/share/applications

	install -d $(DESTDIR)/$(PREFIX)/share/xfce4-panel-profiles
	install -d $(DESTDIR)/$(PREFIX)/share/xfce4-panel-profiles/layouts
	install data/layouts/*.tar.bz2 $(DESTDIR)/$(PREFIX)/share/xfce4-panel-profiles/layouts

	install -d $(DESTDIR)/$(PREFIX)/share/metainfo
	install data/metainfo/*.xml $(DESTDIR)/$(PREFIX)/share/metainfo

	cp -rf locale $(DESTDIR)/$(PREFIX)/share
	ln -sf $(PREFIX)/share/locale $(DESTDIR)/$(PREFIX)/share/$(APPNAME)/locale

uninstall:
	rm -f $(DESTDIR)/$(PREFIX)/share/applications/$(APPNAME).desktop
	rm -rf $(DESTDIR)/$(PREFIX)/share/$(APPNAME)
	rm -rf $(DESTDIR)/$(PREFIX)/share/doc/$(APPNAME)
	# FIXME: Uninstall locales
	rm -f $(DESTDIR)/$(PREFIX)/bin/$(APPNAME)

distcheck: all
	mkdir -p $(APPNAME)-$(VERSION)
	for filename in `git ls-tree -r master --name-only | grep -v git`; do \
		mkdir -p $(APPNAME)-$(VERSION)/`dirname $$filename`; \
		cp $$filename $(APPNAME)-$(VERSION)/$$filename; \
	done;
	tar -cvjSf $(APPNAME)-$(VERSION).tar.bz2 $(APPNAME)-$(VERSION)
	rm -rf $(APPNAME)-$(VERSION)

clean:
	rm -Rf locale
	rm -f xfce4-panel-profiles/*.pyc
	rm -f bin/$(APPNAME).in
	rm -f bin/$(APPNAME)
	rm -f data/metainfo/org.xfce.PanelProfiles.appdata.xml
	rm -f data/layouts/*.tar.bz2
	rm -f xfce4-panel-profiles.desktop
	rm -f Makefile.in
	rm -f Makefile
